﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel.DataAnnotations;
using SilverlightApplication1.ErrorServiceReference;
using BindToolkit.DataAnnotations;
using System.Collections.ObjectModel;

namespace SilverlightApplication1
{
    public partial class MainPage : UserControl
    {
        private Interval interval;
        BindToolkit.BindWrapper wrapper;
        public MainPage()
        {
            InitializeComponent();
            Dictionary<string, object> otherInfos = new Dictionary<string, object>();
            otherInfos["Persons"] = new List<Person>{
                    new Person(){Name="Francesco", Surname="Abbruzzese"}, 
                    new Person{Name="John", Surname="Smith"}, 
                    new Person(){Name="Jack", Surname="Petrone"}};
            interval = new Interval() { InnerInterval = new Interval(), OtherInfos = otherInfos };
            interval.Margin = 5;
            wrapper = new BindToolkit.BindWrapper(interval, true, true);
            this.DataContext = wrapper;
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            wrapper.ValidateWholeObject(null);
        }
        private ValidationException FakeException(string field, string message)
        {
            return new ValidationException(
                new System.ComponentModel.DataAnnotations.ValidationResult(message, new string[] { field }),
                null, null);


        }
        private void InnerGrid_BindingValidationError(object sender, ValidationErrorEventArgs e)
        {
            e.Handled = true;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {

            ErrorServiceClient errorTest = new ErrorServiceClient("BasicHttpBinding_IErrorService");
            errorTest.DoWorkAsync();
            errorTest.DoWorkCompleted +=new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(errorTest_DoWorkCompleted); 
           // wrapper.AddRemoteErrors(new ValidationException[] { FakeException("InnerInterval.Low", "Fake Exception!"), FakeException("OtherInfos[Persons][1].Name", "Fake Exception") });
        }
        private void errorTest_DoWorkCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            Exception ex = e.Error;
            System.ServiceModel.FaultException<ObservableCollection<ValidationErrorInfo>> ve = ex as System.ServiceModel.FaultException<ObservableCollection<ValidationErrorInfo>>;
            if (ve != null) wrapper.AddValidationFaultExceptionContent(ve.Detail);
        }

        

        
    }
}
